function buscarCliente(url) {
    $.ajax({
        url: url,
        contentType: "text/html; charset=UTF-8",
        dataType: "json",
        success: function(data) {
            if (data !== 0) {
                $(data).each(function() {
                    $("#cod").val(this.registros[0].cod_cli);
                    $("#nom").val(this.registros[0].nombre);
                    $("#ocupa").val(this.registros[0].ocupacion);
                    $("#fecNac").val(this.registros[0].fec_nac);
                    $("#lugar").val(this.registros[0].lugar);
                    $("#procedencia").val(this.registros[0].procedencia);
                    $("#rzSocial").val(this.registros[0].razSocial);
                    $("#ruc").val(this.registros[0].ruc);
                    $("#dir").val(this.registros[0].direccion);
                });
            } else {
                alert("Cliente no registrado.");
            }
        }
    });
}

function habitaciones(url) {
    $.ajax({
        url: url,
        contentType: "text/html; charset=UTF-8",
        dataType: "json",
        success: function(data) {
            var tabla = "<table class='table table-striped table-bordered table-hover'> " +
                    "<thead>" +
                    "<tr>" +
                    "<th>Nro. Habitación</th>" +
                    "<th>Descripción</th>" +
                    "<th>Tarifa</th>" +
                    "<th>Piso</th>" +
                    "</tr>" +
                    "</thead>" +
                    "<tbody>";
            $(data).each(function() {
                for (var i = 0; i < this.registros.length; i++) {
                    var idhabi = this.registros[i].id_habi;
                    var nrohabi = this.registros[i].nro_habi;
                    var desctipo = this.registros[i].desc_tipo;
                    var tari = this.registros[i].tarifa;
                    var piso = this.registros[i].piso;
                    var descripcion = String(nrohabi + ' - ' + desctipo);
                    tabla += "<tr class='fil' style='cursor: pointer;' ondblclick='cargardatosHab(" + idhabi + ", " + '"' + descripcion + '"' + ")'>" +
                            "<td>" + nrohabi + "</td>" +
                            "<td>" + desctipo + "</td>" +
                            "<td>" + tari + "</td>" +
                            "<td>" + piso + "</td></tr>";
                }
            });
            tabla += "</tbody></table>";

            $("#habitaciones").html(tabla);
        }
    });
}


function productos(url, id) {
    $.ajax({
        url: url,
        contentType: "text/html; charset=UTF-8",
        dataType: "json",
        success: function(data) {
            var tabla = "<table class='table table-striped table-bordered table-condensed table-hover'> " +
                    "<thead>" +
                    "<tr>" +
                    "<th>Producto</th>" +
                    "<th>Precio</th>" +
                    "</tr>" +
                    "</thead>" +
                    "<tbody>";
            $(data).each(function() {
                for (var i = 0; i < this.registros.length; i++) {
                    var cod = this.registros[i].cod_prod;
                    var desc = this.registros[i].desc_prod;
                    var pre = this.registros[i].pre_prod;
                    tabla += "<tr ondblclick='cargarDatosProd(" + id + "," + cod + "," + '"' + desc + '"' + "," + pre + ")' style='cursor: pointer;' >" +
                            "<td>" + desc + "</td>" +
                            "<td>" + pre + "</td>" +
                            "</tr>";
                }
            });
            tabla += "</tbody></table>";

            $("#tblProd").html(tabla);
        }
    });
}

function cargardatosHab(cod, nro) {
    $("#id_habi").val(cod);
    $("#habi").val(nro);
    $('#habitaciones').dialog('close');

}

function cargardatosHabRes(cod, nro) {
    $("#id_habi").val(cod);
    $("#habi").val(nro);
    $('#habitaciones').slideUp(500);
}

function cargarDatosProd(id, cod, desc, pre) {
    $("#cod" + id).val(cod);
    $("#desc" + id).val(desc);
    $("#prec" + id).val(pre);
    $('#productos').dialog('close');
}

function registroInfo(url) {
    $.ajax({
        url: url,
        contentType: "text/html; charset=UTF-8",
        dataType: "json",
        success: function(data) {
            $(data).each(function() {
                if (this.registros !== null) {
                    var info = '<div class="row-fluid">' +
                            '<div class="span12">' +
                            '<div class="control-group">' +
                            '<p class="alert alert-info" ><strong><i class="icon-ok"></i> Cliente: </strong>' + this.registros[0].nombre + '</p>' +
                            '</div>' +
                            '<div class="control-group">' +
                            '<p class="alert alert-info" ><strong><i class="icon-ok"></i> DNI: </strong>' + this.registros[0].dni + '</p>' +
                            '</div>' +
                            '<div class="control-group">' +
                            '<p class="alert alert-info" ><strong><i class="icon-ok"></i> Habitación: </strong>' + this.registros[0].nro_habi + '</p>' +
                            '</div>' +
                            '<div class="control-group">' +
                            '<p class="alert alert-info" ><strong><i class="icon-ok"></i> Fecha Ingreso: </strong>' + this.registros[0].fec_ing + '</p>' +
                            '</div>' +
                            '<div class="control-group">' +
                            '<p class="alert alert-info" ><strong><i class="icon-ok"></i> Monto Consumido: </strong>' + this.registros[0].monto + '</p>' +
                            '</div>' +
                            '<div class="control-group">' +
                            '<p class="alert alert-info" ><strong><i class="icon-ok"></i> Observación/Comentario: </strong>' + this.registros[0].obs + '</p>' +
                            '</div>' +
                            '</div>' +
                            '</div>';
                
                $('#infoRegistro').html(info);
                }
            });            
        }
    });
}

function filtrarRegistros(url) {
    $.ajax({
        url: url,
        contentType: "text/html; charset=UTF-8",
        dataType: "json",
        success: function(data) {
            var tabla = "";
            $(data).each(function() {
                if (this.registros !== null) {
                    for (var i = 0; i < this.registros.length; i++) {
                        var codReg = this.registros[i].idRegistro;
                        tabla += "<tr onclick='DownUp(" + codReg + ")' style='cursor: pointer;'>" +
                                "<td>" + this.registros[i].nombre + "</td>" +
                                "<td>" + this.registros[i].dni + "</td>" +
                                "<td>" + this.registros[i].nro_habi + "</td>" +
                                "<td>" + this.registros[i].fec_ing + "</td>" +
                                "<td>" + this.registros[i].nom_per + "</td>" +
                                "<td class='td-actions'>" +
                                "<div class='hidden-phone visible-desktop action-buttons'>" +
                                "<a class='blue' href='#'><i class='icon-list-alt bigger-150' title='Detalle'></i></a>" +
                                "</div>" +
                                "</td>" +
                                "</tr>" +
                                "<tr id='" + codReg + "' class='detalle' style='display: none;'>" +
                                "<td colspan='6'>" +
                                "<div id='det" + codReg + "' style='background-color: #ebebeb; border: 3px solid #cfcfcf; padding: 10px;'>" +
                                "<legend>Consumo productos</legend>" +
                                "<div class='row-fluid'>" +
                                "<div class='span12'>" +
                                "<div class='widget-box collapsed'>" +
                                "<div class='widget-header widget-header-small header-color-orange' style='cursor: pointer;' onclick='desplegar(" + codReg + ");'>" +
                                "<h6><i class='icon-sort'></i>Agregar Producto</h6>" +
                                "<span class='right-align'>Cliente: " + this.registros[i].nombre + "</span>" +
                                "<div class='widget-toolbar'>" +
                                "<a href='#'><i class='icon-chevron-down'></i></a>" +
                                "</div></div>" +
                                "<div class='widget-body' id='cuerpo" + codReg + "'>" +
                                "<div class='widget-main'>" +
                                "<form action='#' class='form-horizontal'>" +
                                "<div class='row-fluid'>" +
                                "<div class='span6'>" +
                                "<div class='control-group'>" +
                                "<label class='control-label' for='nom'>Producto</label>" +
                                "<div class='controls'>" +
                                "<input type='hidden' id='cod" + codReg + "'>" +
                                "<input type='text'  id='desc" + codReg + "'>" +
                                "<button class='blue btn btn-small btn-primary' onclick='abrirProductos(" + codReg + ")' type='button'>" +
                                "<i class='icon-search'></i>" +
                                "</button>" +
                                "</div></div>" +
                                "<div class='control-group'>" +
                                "<label class='control-label' for='prec" + codReg + "'>Precio</label>" +
                                "<div class='controls'>" +
                                "<input type='text' id='prec" + codReg + "' class='span3' >" +
                                "</div></div>" +
                                "</div>" +
                                "<div class='span6'>" +
                                "<div class='control-group'>" +
                                "<label class='control-label' for='canti" + codReg + "'>Cantidad</label>" +
                                "<div class='controls'>" +
                                "<input type='text' id='canti" + codReg + "' oninput='calcsubtotal(" + codReg + ")' class='span3'>" +
                                "</div></div>" +
                                "<div class='control-group'>" +
                                "<label class='control-label' for='subTotal" + codReg + "'>Sub Total</label>" +
                                "<div class='controls'>" +
                                "<input type='text' id='subTotal" + codReg + "' maxlength='8' class='span3'>" +
                                "</div></div></div>" +
                                "<div class='span11'>" +
                                "<div class='form-actions'>" +
                                "<div class='width-100'></div>" +
                                "<button class='btn btn-info btnregistrar' onclick='agregar(" + codReg + ")' type='button'>" +
                                "<i class='icon-ok bigger-110'></i>" +
                                "Agregar" +
                                "</button>" +
                                "&nbsp; &nbsp; &nbsp;" +
                                "<button class='btn' type='reset'>" +
                                "<i class='icon-undo bigger-110'></i>" +
                                "Limpiar" +
                                "</button>" +
                                "</div>" +
                                "</div>" +
                                "</div>" +
                                "</form>" +
                                "</div>" +
                                "</div>" +
                                "</div>" +
                                "</div>" +
                                "</div>" +
                                "<div class='row-fluid'>" +
                                "<div class='span12'>" +
                                "<div class='widget-box'>" +
                                "<div class='widget-header header-color-orange'>" +
                                "<h5 class='bigger lighter'>" +
                                "<i class='icon-table'></i>" +
                                "Detalle Registro" +
                                "</h5>" +
                                "</div>" +
                                "<div class='widget-body'>" +
                                "<div class='widget-main no-padding'>" +
                                "<table id='tabla" + codReg + "' class='table table-striped table-bordered'>" +
                                "<thead>" +
                                "<tr>" +
                                "<th style='display: none;'>Código</th>" +
                                "<th>Producto</th>" +
                                "<th>Precio</th>" +
                                "<th>Cantidad</th>" +
                                "<th>Sub Total</th>" +
                                "<th>Fecha</th>" +
                                "<th>Acciones</th>" +
                                "</tr>" +
                                "</thead>" +
                                "<tbody>" +
                                "</tbody>" +
                                "</table>" +
                                "<div style='float: right; margin-top: 5px; color: blue; font-weight: bold'></div>" +
                                "</div>" +
                                "</div>" +
                                "</div>" +
                                "</div>" +
                                "</div>" +
                                "<div>" +
                                "</div><div class='space-10'></div>" +
                                "<legend>Cierre de Alquiler de Habitación</legend>" +
                                "<div style='overflow: hidden'>" +
                                "<div class='span12'>" +
                                "<div class='widget-box'>" +
                                "<div class='widget-header widget-header-small header-color-orange'>" +
                                "<h5 class='bigger lighter'>" +
                                "<i class='icon-table'></i>" +
                                "Montos" +
                                "</h5>" +
                                "</div>" +
                                "<div class='widget-body'>" +
                                "<div class='widget-main'>" +
                                "<form action='http://localhost/SanIsidroAPP/intranet/reserva/cerrarRegistro/" + codReg + "' class='form-horizontal'>" +
                                "<div class='row' style='margin-left: 20px'>" +
                                "<div class='span6'>" +
                                "<div class='control-group'>" +
                                "<label class='control-label' for='totalProd" + codReg + "'>Consumo Productos</label>" +
                                "<div class='controls'>" +
                                "<div class='input-prepend'>" +
                                "<span class='add-on'>" +
                                "<i class='icon-dollar'></i>" +
                                "</span>" +
                                "<input type='text'  id='totalProd" + codReg + "' class='span5 total'>" +
                                "</div>" +
                                "</div>" +
                                "</div>" +
                                "<div class='control-group'>" +
                                "<label class='control-label' for='totalHabit" + codReg + "'>Consumo Habitación</label>" +
                                "<div class='controls'>" +
                                "<div class='input-prepend'>" +
                                "<span class='add-on'>" +
                                "<i class='icon-dollar'></i>" +
                                "</span>" +
                                "<input type='text' id='totalHabit" + codReg + "' value='0' class='span5' >" +
                                "</div>" +
                                "<span style='margin-left: -88px'>" +
                                "<button class='blue btn btn-small btn-primary' onclick='abrirDetHab(" + codReg + ");' type='button'>" +
                                "<i class='icon-eye-open'></i>" +
                                "</button>" +
                                "</span>" +
                                "</div> " +
                                "</div>" +
                                "<hr>" +
                                "<div class='control-group'>" +
                                "<label class='control-label' for='montoTotal" + codReg + "'>Monto Total</label>" +
                                "<div class='controls'>" +
                                "<div class='input-prepend'>" +
                                "<span class='add-on'>" +
                                "<i class='icon-dollar'></i>" +
                                "</span>" +
                                "<input type='text' value='0' id='montoTotal" + codReg + "' class='span5' >" +
                                "</div>" +
                                "<span style='margin-left: -88px'>" +
                                "<button class='blue btn btn-small btn-primary' onclick='calcMontoTotal(" + codReg + ")' type='button' title='Calcular'>" +
                                "<i class='icon-cog'></i>" +
                                "</button>" +
                                "</span>" +
                                "</div>" +
                                "</div>" +
                                "</div>" +
                                "<div class='span11'>" +
                                "<div class='form-actions'>" +
                                "<div class='width-100'></div>" +
                                "<button class='btn btn-info' onclick='' type='submit'>" +
                                "<i class='icon-ok bigger-110'></i>" +
                                "Finalizar" +
                                '</button>' +
                                "</div>" +
                                "</div>" +
                                "</div>" +
                                "</form>" +
                                "</div>" +
                                "</div>" +
                                "</div>" +
                                "</div>" +
                                "</div>" +
                                "</div>" +
                                "</td></tr>";
                    }
                }
            });
            $("#datosRegistro").html(tabla);
        }
    });
}